<?php
/**
 * Autor:   Miguel A. Zilli
 * Email:   miguelzilli@gmail.com
 */

class Financiamiento extends AppModel {
    var $name = 'Financiamiento';
    var $useTable = 'financiamientos';
    var $primaryKey = 'id';
    var $displayField = 'tipo_fin';
    var $recursive = 0;
    var $order=array(
        'Financiamiento.tipo_fin'=>'ASC'
    );

    var $validate = array(
        'tipo_fin' =>array(
            array('rule'=>'notEmpty','message'=>'Debe completar este dato.')
        ),
        'monto_fin' =>array(
            array('rule'=>'numeric','message'=>'Solo se permiten números.','allowEmpty' => true)
        )
    );

    var $belongsTo = array(
        'Entidad' => array(
            'className' => 'Entidad',
            'foreignKey' => 'entidad_id'
        ),
        'Revision' => array(
            'className' => 'Revision',
            'foreignKey' => 'revision_id'
        )
    );

    public function beforeSave($options = array()){
        $this->data['Financiamiento']['fch_ent_fin'] = $this->formatDate($this->data['Financiamiento']['fch_ent_fin']);
        if(!empty($this->data['Financiamiento'])){
            foreach($this->data['Financiamiento'] as $campo=>$valor){
                $this->data['Financiamiento'][$campo]=strtoupper($valor);
            }
        }
        if(empty($this->data['Financiamiento']['fch_ent_fin'])){
            $this->data['Financiamiento']['fch_ent_fin'] = NULL;
        }
        return parent::beforeSave($options);
    }

    public function afterFind($result) {
        foreach ($result as $clave => $valor) {
            if (isset($valor['Financiamiento']['fch_ent_fin'])) {
                $result[$clave]['Financiamiento']['fch_ent_fin'] = $this->formatoFechatAfterFind($valor['Financiamiento']['fch_ent_fin']);
            }
        }
        return parent::afterFind($result);
    }

    function formatoFechatAfterFind($fecha) {
        return date('d-m-Y', strtotime($fecha));
    }

    function formatDate($dateToFormat){
        $pattern1 = '/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/i';
        $pattern2 = '/^([0-9]{4})\/([0-9]{2})\/([0-9]{2})$/i';
        $pattern3 = '/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/i';
        $pattern4 = '/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/i';

        $coincidences = array();

        if(preg_match($pattern1, $dateToFormat)){
            $newDate = $dateToFormat;
        }elseif(preg_match($pattern2, $dateToFormat, $coincidences)){
            $newDate = $coincidences[1] . '-' . $coincidences[2] . '-' . $coincidences[3];
        }elseif(preg_match($pattern3, $dateToFormat, $coincidences)){
            $newDate = $coincidences[3] . '-' . $coincidences[2] . '-' . $coincidences[1];
        }elseif(preg_match($pattern4, $dateToFormat, $coincidences)){
            $newDate = $coincidences[3] . '-' . $coincidences[2] . '-' . $coincidences[1];
        }else{
            $newDate = null;
        }
        return $newDate;
    }
}
?>